{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8d245852",
   "metadata": {},
   "source": [
    "# 1保存整个模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57c4ce40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init Plugin\n",
      "Init Graph Optimizer\n",
      "Init Kernel\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "93ce5d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dedcb956",
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_image, train_label),(test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16e0eaea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(((60000, 28, 28), (60000,)), ((10000, 28, 28), (10000,)))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(train_image.shape, train_label.shape),(test_image.shape, test_label.shape) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0c93820d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x2971c56a0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAATSklEQVR4nO3de3Bc5XkG8OfRaiVZvtvCF4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dV/q/N+7pxxNeR50+WyIpHQFXQikVDYRSKhsItEQmEXiYTCLhIJhV0kEgq7SCT+FwFV93oyHeAmAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(train_image[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e771768",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([9, 0, 0, ..., 3, 0, 5], dtype=uint8), 255)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(train_label,np.max(train_image[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "10f71dc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_image =train_image/ 255\n",
    "test_image =test_image/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1c2dc635",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metal device set to: Apple M1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 17:03:47.820029: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
      "2021-10-09 17:03:47.820411: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
     ]
    }
   ],
   "source": [
    "model=tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n",
    "model.add(tf.keras.layers.Dense(128,activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(10,activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6616c6e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten (Flatten)            (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 128)               100480    \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8c64f73c",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(\n",
    "    optimizer='adam',\n",
    "    loss='sparse_categorical_crossentropy',\n",
    "    metrics=['acc']\n",
    "    \n",
    "    \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4a958ca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 17:06:58.878323: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)\n",
      "2021-10-09 17:06:58.878798: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n",
      "2021-10-09 17:06:58.973230: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/3\n",
      "1875/1875 [==============================] - 10s 4ms/step - loss: 0.5003 - acc: 0.8234\n",
      "Epoch 2/3\n",
      "1875/1875 [==============================] - 9s 5ms/step - loss: 0.3749 - acc: 0.8651\n",
      "Epoch 3/3\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.3371 - acc: 0.8770\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2ba939850>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_image,train_label,epochs=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c075aff1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.3609241545200348, 0.8716000318527222]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b260c51e",
   "metadata": {},
   "source": [
    "## 保存整个模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6b77fcd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('less_model.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47af7057",
   "metadata": {},
   "source": [
    "## 加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c4583608",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_model=tf.keras.models.load_model('less_model.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c3d02ec4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten (Flatten)            (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 128)               100480    \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "new_model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "480b64af",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 17:19:55.239610: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.3609241545200348, 0.8716000318527222]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d622bd",
   "metadata": {},
   "source": [
    "## 保存了：1权重值   2模型配置（架构）  3优化器配置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb2dfc66",
   "metadata": {},
   "source": [
    "# 2仅保存架构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c558f9a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_config = model.to_json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "33ae37dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"class_name\": \"Sequential\", \"config\": {\"name\": \"sequential\", \"layers\": [{\"class_name\": \"InputLayer\", \"config\": {\"batch_input_shape\": [null, 28, 28], \"dtype\": \"float32\", \"sparse\": false, \"ragged\": false, \"name\": \"flatten_input\"}}, {\"class_name\": \"Flatten\", \"config\": {\"name\": \"flatten\", \"trainable\": true, \"batch_input_shape\": [null, 28, 28], \"dtype\": \"float32\", \"data_format\": \"channels_last\"}}, {\"class_name\": \"Dense\", \"config\": {\"name\": \"dense\", \"trainable\": true, \"dtype\": \"float32\", \"units\": 128, \"activation\": \"relu\", \"use_bias\": true, \"kernel_initializer\": {\"class_name\": \"GlorotUniform\", \"config\": {\"seed\": null}}, \"bias_initializer\": {\"class_name\": \"Zeros\", \"config\": {}}, \"kernel_regularizer\": null, \"bias_regularizer\": null, \"activity_regularizer\": null, \"kernel_constraint\": null, \"bias_constraint\": null}}, {\"class_name\": \"Dense\", \"config\": {\"name\": \"dense_1\", \"trainable\": true, \"dtype\": \"float32\", \"units\": 10, \"activation\": \"softmax\", \"use_bias\": true, \"kernel_initializer\": {\"class_name\": \"GlorotUniform\", \"config\": {\"seed\": null}}, \"bias_initializer\": {\"class_name\": \"Zeros\", \"config\": {}}, \"kernel_regularizer\": null, \"bias_regularizer\": null, \"activity_regularizer\": null, \"kernel_constraint\": null, \"bias_constraint\": null}}]}, \"keras_version\": \"2.5.0\", \"backend\": \"tensorflow\"}'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json_config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8f5e7254",
   "metadata": {},
   "outputs": [],
   "source": [
    "reinitialized_model = tf.keras.models.model_from_json(json_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "793aa3f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten (Flatten)            (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 128)               100480    \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "reinitialized_model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a5ef96c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "reinitialized_model.compile(\n",
    "    optimizer='adam',\n",
    "    loss='sparse_categorical_crossentropy',\n",
    "    metrics=['acc']\n",
    "   \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5b10c44e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 17:39:05.050511: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[2.4021365642547607, 0.12939999997615814]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reinitialized_model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e34146a",
   "metadata": {},
   "source": [
    "## 说明没保存权重和配置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba9b413d",
   "metadata": {},
   "source": [
    "# 3仅保存权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "075a8e37",
   "metadata": {},
   "outputs": [],
   "source": [
    "weights=model.get_weights()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b06ce75d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[-0.04342262, -0.20733304,  0.0175512 , ..., -0.01924818,\n",
       "          0.01197501,  0.00822553],\n",
       "        [-0.07615548,  0.00647427,  0.01982315, ...,  0.01932318,\n",
       "         -0.08467223, -0.05459651],\n",
       "        [ 0.00851883, -0.0910786 , -0.01265913, ..., -0.03602001,\n",
       "         -0.02849253,  0.00047732],\n",
       "        ...,\n",
       "        [ 0.1267783 , -0.07299199, -0.08402987, ..., -0.10918875,\n",
       "         -0.07649646, -0.03203431],\n",
       "        [ 0.03643153, -0.08327249,  0.01981998, ..., -0.10651643,\n",
       "          0.00777264, -0.04277816],\n",
       "        [ 0.05253803, -0.04642901, -0.06948322, ..., -0.03407931,\n",
       "         -0.04575009,  0.00797548]], dtype=float32),\n",
       " array([-0.03470124, -0.18857834,  0.02988227, -0.1613791 , -0.17098348,\n",
       "        -0.00837328,  0.17192173, -0.05707675, -0.06309133,  0.21865743,\n",
       "        -0.26138207,  0.11049752, -0.09199715,  0.00666685,  0.36924478,\n",
       "         0.2454025 , -0.03516789,  0.03696366,  0.17906126, -0.26055554,\n",
       "         0.24262919,  0.06433845,  0.43015224, -0.01202999,  0.32615212,\n",
       "         0.27690613,  0.27765056,  0.33632   ,  0.23061858,  0.37970108,\n",
       "         0.26217887,  0.00776381, -0.01945434,  0.10741722,  0.0918089 ,\n",
       "         0.03787745,  0.03656667,  0.35936406, -0.11641403,  0.01775327,\n",
       "         0.21669021,  0.05541982,  0.14795168,  0.30230722,  0.26139942,\n",
       "         0.24931611,  0.15029193,  0.5265866 , -0.17345992,  0.00812555,\n",
       "         0.24233916,  0.08876411, -0.04996275,  0.19077721,  0.08493241,\n",
       "         0.34540752,  0.24164785, -0.17030326,  0.09153695,  0.27718544,\n",
       "        -0.13524245, -0.04491818, -0.17247647,  0.33894992,  0.4377081 ,\n",
       "        -0.07646382, -0.29008272,  0.21449615, -0.02466529,  0.34590718,\n",
       "         0.02115358, -0.01083764, -0.01538886,  0.15595368,  0.07837489,\n",
       "         0.19583234, -0.2548994 ,  0.1339097 ,  0.22661012,  0.22124071,\n",
       "        -0.03555334,  0.28396007,  0.03541822,  0.17182356,  0.11750295,\n",
       "        -0.26053104,  0.23400618,  0.01120963,  0.20493783,  0.22099623,\n",
       "         0.30477902,  0.07301964,  0.04785219, -0.11159565,  0.19638996,\n",
       "         0.2626493 ,  0.0455859 ,  0.09313258,  0.11627229,  0.34839308,\n",
       "        -0.18569088, -0.013524  , -0.00489561,  0.00481006,  0.01323025,\n",
       "         0.4361017 , -0.1830758 , -0.01157541,  0.24509329,  0.22009155,\n",
       "        -0.21999702,  0.06675448,  0.14571562,  0.17548762,  0.46173307,\n",
       "         0.02782764, -0.01532367,  0.00281995,  0.21276781, -0.1535571 ,\n",
       "        -0.22457725, -0.11626743,  0.17798331,  0.2692926 , -0.15159903,\n",
       "         0.01245221,  0.19288684,  0.04018182], dtype=float32),\n",
       " array([[-0.11257786, -0.15570258,  0.04031914, ..., -0.21552026,\n",
       "          0.09111504,  0.1083591 ],\n",
       "        [ 0.11166168, -0.01893447, -0.07483657, ..., -0.147033  ,\n",
       "         -0.00630703,  0.19375873],\n",
       "        [-0.09638516, -0.13595408, -0.19355814, ...,  0.05539432,\n",
       "         -0.06052831,  0.00462452],\n",
       "        ...,\n",
       "        [ 0.147042  , -0.22286542, -0.22355492, ..., -0.16486834,\n",
       "         -0.20511985, -0.0049064 ],\n",
       "        [ 0.24482752, -0.03082743, -0.45030695, ..., -0.45304388,\n",
       "         -0.32386932,  0.02923215],\n",
       "        [ 0.17607729,  0.2513667 , -0.02172277, ..., -0.03657852,\n",
       "         -0.10415815, -0.27933395]], dtype=float32),\n",
       " array([ 0.05406864, -0.2731635 ,  0.08269646,  0.18301168, -0.25074285,\n",
       "         0.205705  ,  0.13703723,  0.08063456, -0.17096607, -0.3568095 ],\n",
       "       dtype=float32)]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weights"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "323cd049",
   "metadata": {},
   "source": [
    "## 加载权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "556ca31c",
   "metadata": {},
   "outputs": [],
   "source": [
    "reinitialized_model.set_weights(weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "616cb9e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.3609241545200348, 0.8716000318527222]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reinitialized_model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f07dd66",
   "metadata": {},
   "source": [
    "### 保存到磁盘上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e0ff62f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_weights('less_weights.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c2c5bf73",
   "metadata": {},
   "outputs": [],
   "source": [
    "reinitialized_model.load_weights('less_weights.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "dbd818fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.3609241545200348, 0.8716000318527222]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reinitialized_model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a857aaee",
   "metadata": {},
   "source": [
    "# 4在训练的时候保存检查点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a4a839e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "checkpoint_path='cp.ckpt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "41681ce4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cp_callback=tf.keras.callbacks.ModelCheckpoint(\n",
    "    checkpoint_path,\n",
    "    save_weights_only=True #如果是fasle就保存整个模型\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9fc267c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n",
    "model.add(tf.keras.layers.Dense(128,activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(10,activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "905965cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(\n",
    "    optimizer='adam',\n",
    "    loss='sparse_categorical_crossentropy',\n",
    "    metrics=['acc']\n",
    "    \n",
    "    \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4c20ca51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/3\n",
      "  34/1875 [..............................] - ETA: 8s - loss: 1.2402 - acc: 0.5717"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 18:03:03.552153: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1875/1875 [==============================] - 9s 5ms/step - loss: 0.4963 - acc: 0.8257\n",
      "Epoch 2/3\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.3754 - acc: 0.8644\n",
      "Epoch 3/3\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.3373 - acc: 0.8769\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2b6e36b80>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_image,train_label,epochs=3,callbacks=[cp_callback])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3860f4b",
   "metadata": {},
   "source": [
    "## 加载检查点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ef56d2cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x2dbed0760>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.load_weights(checkpoint_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9fc33276",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-09 18:07:36.694849: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.37637048959732056, 0.8633000254631042]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_image,test_label,verbose=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40345116",
   "metadata": {},
   "source": [
    "# 自定义模型保存检查点 后续"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2e8275a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
